package com.qzsoft.zhjd.materiel.modules.materielsupplierdetailed.service.impl;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qzsoft.zhjd.common.annotation.ZhjdDataFilter;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.common.excel.ZhjdExcelExportUtils;
import com.qzsoft.zhjd.common.excel.style.ZhjdExcelExportStyler;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;
import com.qzsoft.zhjd.materiel.modules.materieldata.dto.MaterielDataListDto;
import com.qzsoft.zhjd.materiel.modules.materieldata.enmus.WeightTypeEnum;
import com.qzsoft.zhjd.materiel.modules.materieldata.service.MaterielDataService;
import com.qzsoft.zhjd.materiel.modules.materielsupplierdetailed.dto.MaterielSupplierDetailedDto;
import com.qzsoft.zhjd.materiel.modules.materielsupplierdetailed.mapper.MaterielSupplierDetailedMapper;
import com.qzsoft.zhjd.materiel.modules.materielsupplierdetailed.service.MaterielSupplierDetailedService;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName MaterielSupplierDetailedServiceImpl
 * @Description 供货商供货情况分析业务接口实现
 * @author linhuawei
 * @since 2022-04-01
 */
@Slf4j
@Service
public class MaterielSupplierDetailedServiceImpl implements MaterielSupplierDetailedService {

    @Autowired
    private MaterielSupplierDetailedMapper materielSupplierDetailedMapper;

    @Autowired
    private MaterielDataService materielDataService;

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public IPage<MaterielSupplierDetailedDto> page(Map<String, Object> params) {
        IPage<MaterielSupplierDetailedDto> page = baseServiceComponent.getPage(params,
                MaterielSupplierDetailedDto.class);
        return materielSupplierDetailedMapper.queryDto(page, params);
    }

    @Override
    public IPage<MaterielDataListDto> pageForSupplier(Map<String, Object> params) {
        params.put("weightType", WeightTypeEnum.RECEIVE.getValue());// 收料
        return materielDataService.pageDto(params);
    }

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public void export(Map<String, Object> params, HttpServletResponse response) {
        String fileName = "供应商供货明细";
        List<MaterielSupplierDetailedDto> list = materielSupplierDetailedMapper.queryDto(params);
        ExportParams exportParams = new ExportParams(null, fileName);
        exportParams.setFixedTitle(false);
        exportParams.setStyle(ZhjdExcelExportStyler.class);
        try {
            ZhjdExcelExportUtils.exportExcel(response, fileName, exportParams, MaterielSupplierDetailedDto.class, list);
        } catch (IOException e) {
            log.error("供应商供货明细导出失败：{}", e);
            RenExceptionUtils.throwEx("数据导出失败");
        }
    }

}
